<?php
declare (strict_types=1);

namespace app\index\controller;

use app\common\tool\Result;
use think\facade\View;
use think\Response;
use app\common\model\Transfer as TransferModel;
use think\exception\ValidateException;

class Transfer extends Base
{

    public function index()
    {
        return View::fetch();
    }

    public function record()
    {
        return View::fetch();
    }

    public function getList(): Response
    {

        $map = ['uid|take_uid' => $this->user->id];
        $list = TransferModel::getPageList($map, '*', $this->getPage(), 20, false, ['take', 'user']);

        foreach ($list['data'] as &$v) {
            if ($v->uid == $this->user->id) {
                $v->trStatus = '转出';
                $v->trUser = $v->take->code;
            } else {
                $v->trStatus = '转入';
                $v->trUser = $v->user->code;
            }
        }
        return Result::fastReturn($list);
    }

    public function verify(): Response
    {
        $code = $this->request->get('code');
        $nickname = \app\common\model\User::where('code|mobile', $code)->value('nickname');
        if ($nickname) {
            return Result::success('success', $nickname);
        } else {
            return Result::fail('会员不存在', '会员不存在');
        }
    }

    public function save(): Response
    {
        $param = $this->request->post('', '', $this->filter);
        try {
            $this->validate($param, 'Transfer');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
        return Result::fastReturn(TransferModel::add($param, $this->user, $this->system));
    }

}

